#include "sysgnmainwindow.h"
#include <QSplitter>
#include <QTreeView>
#include <QTextBrowser>
#include <QHBoxLayout>
#include <QFile>
#include <mainwindow.h>

CNS_SYS_REGISTER(SysGnMainWindow);

SysGnMainWindow::SysGnMainWindow()
{

}

/*void setStyle(const QString &fileName)
{
    if (!fileName.isEmpty()) {
        QFile file(fileName);

        if (file.open(QFile::ReadOnly)) {
            QString str = file.readAll();
            QString paletteColor = str.mid(20, 7);
            qApp->setPalette(QPalette(QColor(paletteColor)));
            qApp->setStyleSheet(str);
        }
    }
}*/

void SysGnMainWindow::f_start(CnsComponentPtr com)
{
    MainWindowPtr mainWindow = com->propertyInNs("mainWindow").value<MainWindowPtr>();
    QWidget * widget = mainWindow->centralWidget();

    QSplitter *pSplitter = new QSplitter(Qt::Orientation::Horizontal, widget);
    const QString &treeViewName = com->propertyInNs("treeViewName").toString();
    const QString &textBrowserName = com->propertyInNs("TextBrowserName").toString();
    QTreeView* pTreeView = mainWindow->findChild<QTreeView*>(treeViewName);
    QTextBrowser* pTextBrower = mainWindow->findChild<QTextBrowser*>(textBrowserName);

    QHBoxLayout * pHBox = static_cast<QHBoxLayout*>(widget->layout());

    pSplitter->addWidget(pTreeView);
    pSplitter->addWidget(pTextBrower);
    pHBox->addWidget(pSplitter);

//    setStyle(":/qss/black.css");

    mainWindow->show();
}

void SysGnMainWindow::f_handleGnFileData(CnsComponentPtr com, QNetworkReply *networkReply)
{
    MainWindowPtr lgDialog = com->property("mainWindow").value<MainWindowPtr>();
    if (!networkReply) {
        CNSLOG_ERROR(QString("NetworkReply is false in handleGnFileData!"));
        return;
    }

    if (!networkReply->error()) {
        const QByteArray& text = networkReply->readAll();
        MainWindowPtr mainWindow = com->propertyInNs("mainWindow").value<MainWindowPtr>();
        const QString &textBrowserName = com->propertyInNs("TextBrowserName").toString();
        QTextBrowser* pTextBrower = mainWindow->findChild<QTextBrowser*>(textBrowserName);
        pTextBrower->setText(text);
    } else {
        CNSLOG_ERROR(QString("NetworkReply has error in handlePushFileFilterData!"));
    }
    networkReply->deleteLater();
}
